/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.darksigns.webstat.back.ejb;

import cl.darksigns.webstat.back.vo.WebStatVO;
import cl.darksigns.webstat.back.utils.ProcessData;

import cl.darksigns.webstat.back.vo.WebStatSpecificVO;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import org.apache.log4j.Logger;

/**
 *
 * @author _ViKtOr
 */
@MessageDriven(mappedName = "jms/WebStatsReceiveQueue", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class ProcessMessageBean implements MessageListener {

    Logger logger = Logger.getLogger(ProcessMessageBean.class);

    public ProcessMessageBean() {
    }

    public void onMessage(Message message) {
        logger.debug("Instance of: " + message.getClass());

        if (message != null) {
            if (message instanceof ObjectMessage) {
                ObjectMessage msg;
                try {
                    msg = (ObjectMessage) message;
                     logger.info("MSG class: " + msg.getObject().getClass().toString());
                    if (msg.getObject().getClass().toString().equalsIgnoreCase("class cl.darksigns.webstat.back.vo.WebStatVO")) {
                         WebStatVO webstat = null;
                        logger.info("MSG class: " + msg.getObject().getClass());
                        webstat = (WebStatVO) msg.getObject();
                        ProcessData process = ProcessData.getInstance();
                        process.processInformation(webstat);
                        logger.info(webstat.getRequestValues().getReqEventId());
                    }else if(msg.getObject().getClass().toString().equalsIgnoreCase("class cl.darksigns.webstat.back.vo.WebStatSpecificVO")){
                        logger.info("This is an specific message");
                        WebStatSpecificVO webstat = null;
                        webstat = (WebStatSpecificVO)msg.getObject();
                        
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e);
                }
            } else {
                logger.info("The message type is not correct!");
            }
        } else {

        }

    }
}
